<script setup>
import { onMounted, reactive, ref, watch } from 'vue'
import { getStatusInfo } from '@u/ststus-color'
import { backBatch, queryList, queryRegionList } from '@api/question'
import emitter from '@u/event-bus'
import { Message } from 'element-ui'

const props = defineProps({
  areaIds: {
    type: Object,
    default: () => {},
  },
  type: {
    type: String,
    default: 'region',
  },
  status: {
    type: [Number, String],
    default: '',
  },
  regionType: {
    type: String,
    default: '',
  },
})

const statisticTableProp = ref([
  {
    name: '区域',
    prop: 'name',
  },
  {
    name: '更新时间',
    prop: 'updateTime',
  },
  {
    name: '状态',
    prop: 'status',
  },
])

const listData = ref([])
const listTotal = ref(0)

const getStatusText = (item) => {
  const type = 'residence'
  if (props.type === 'region-fill' && item.status === 2) {
    return 3
  }
  if (item.status || item.status === 0) {
    return item.status
  } else {
    return item.residenceStatus
  }
}

const form = reactive({
  pageSize: 10,
  pageIndex: 1,
})

const getList = () => {
  // console.log(props.areaIds)
  if (props.type === 'satisfaction') {
    return
  }
  if (props.type === 'region-fill') {
    getRegionList()
    return
  }
  queryList(
    {
      ...form,
      ...props.areaIds,
      status: props.status,
    },
    props.type
  ).then((res) => {
    if (res.success) {
      listData.value = res.data.list.map((item) => {
        return {
          ...item,
          name: getName(item),
          status: getStatusText(item),
          updateTime: item.updateTime || item.createTime || item.createtime,
        }
      })
      listTotal.value = res.data.count
    }
  })
}

const getRegionList = () => {
  let params = {
    ...form,
    regionId: props.areaIds.regionId,
    // status: props.status,
    type: props.regionType,
  }
  if (props.regionType === 'region-community') {
    params.status = props.status
  }
  queryRegionList(params).then((res) => {
    if (res.success) {
      listData.value = res.data.list.map((item) => {
        return {
          ...item,
          name: getName(item),
          status: getStatusText(item),
          updateTime: item.updateTime || item.createTime || item.createtime,
        }
      })
      listTotal.value = res.data.count
    }
  })
}

const getName = (item) => {
  if (props.regionType === 'region-community') {
    return item.communityname
  }
  let name = ''
  if (item.name) {
    return item.name
  }
  if (item.regionName) {
    name += item.regionName
  }
  if (item.streetName) {
    name += item.streetName
  }
  if (item.communityName) {
    name += item.communityName
  }
  return name
}

onMounted(() => {
  getList()
})

const handleChangePage = (page) => {
  form.pageIndex = page
  getList()
}

const refresh = () => {
  form.pageIndex = 1
  getList()
}

const handleClickTable = (row) => {
  if (props.regionType === 'region-community') {
    emitter.emit('tabDetail', {
      pkid: row.relation_id,
      relationId: row.relation_id,
      status: row.status,
      type: 'region-community',
      name: row.communityname,
    })
    return
  }
  emitter.emit('tabDetail', row)
}

const tableRef = ref()
const selectData = ref([])
const handleSelectAll = (selection) => {
  const findData = selection.filter((row) => {
    return row.status !== 2 && row.status !== -1
  })
  findData.map((row) => {
    tableRef.value.toggleRowSelection(row, false)
    Message.closeAll()
    Message({
      message: '只能选择待审核或已通过的数据',
      type: 'warning',
    })
  })
  selectData.value = selection
}

const getResourceTypeByType = (type) => {
  let _type = 2
  switch (type) {
    case 'residence':
      _type = 2
      break
    case 'community':
      _type = 3
      break
    case 'street':
      _type = 4
      break
    case 'region':
      _type = 5
      break
  }
  return _type
}

const backData = () => {
  const backIds = selectData.value
    .filter((row) => {
      return row.status === -1 || row.status === 2
    })
    .map((item) => {
      return item.residenceId || item.relationId
    })
  backBatch({
    resourceIds: backIds,
    resourceType: getResourceTypeByType(props.type),
  }).then((res) => {
    if (res.success) {
      Message.success('退回成功')
      refresh()
    } else {
      Message.error(res.message)
    }
  })
}

defineExpose({
  refresh,
  backData,
})
</script>

<template>
  <div class="statistics-list">
    <el-table
      ref="tableRef"
      :data="listData"
      row-key="pkid"
      :select-on-indeterminate="false"
      @row-click="handleClickTable"
      @selection-change="handleSelectAll"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column
        v-for="(item, index) in statisticTableProp"
        :key="index"
        :label="item.name"
        :prop="item.prop"
      >
        <template #default="scope">
          <template v-if="item.prop === 'status'">
            <div
              :style="{
                color: getStatusInfo(scope.row.status).color,
              }"
            >
              {{ getStatusInfo(scope.row.status).statusText }}
            </div>
          </template>
          <template v-else>
            {{ scope.row[item.prop] }}
          </template>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      :total="listTotal"
      :page-size="form.pageSize"
      :current-page="form.pageIndex"
      @current-change="handleChangePage"
    >
    </el-pagination>
  </div>
</template>

<style scoped lang="scss">
.status-statistics {
  margin-top: 10px;
  display: flex;
  background: rgb(241, 245, 250);
  align-items: center;
  height: 60px;
  justify-content: space-around;

  .statistic-item {
    .label {
      display: flex;
      align-items: center;
      font-size: 14px;

      .icon {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        margin-right: 4px;
      }
    }

    .value {
      width: 100%;
      text-align: center;
    }
  }
}

.statistics-list {
  margin-top: 10px;

  .statistic-card {
    position: relative;
    height: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    border-color: rgb(220, 224, 231);
    border-width: 1px;
    border-style: solid;
    border-radius: 3px;
    margin-bottom: 12px;
    padding: 0 6px;

    .status-area {
      line-height: 18px;
      text-align: center;
      position: absolute;
      right: 16px;
      top: 10px;
      width: 50px;
      height: 18px;
      border-radius: 3px;
      font-size: 14px;
    }
  }
}
</style>
